
public class Singleton extends Thread {
	
	private static Singleton instance;
	private static int nbInstance;
	private static int nbCall;
	
	public int getNbInstance() {
		return nbInstance;
	}
	
	public int getNbCall() {
		return nbCall;
	}
	
	private Singleton() {
		nbInstance++;
	}
	
	public static Singleton newSingleton() {
		nbCall++;
		if (instance == null) {
			try {
				sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			instance = new Singleton();
		}
		return instance;
	}
	
	public static Singleton newSingletonBis() {
		nbCall++;
		if (instance == null) {
			instance = new Singleton();
		}
		return instance;
	}
	
	@Override
	public void run() {
		System.out.println("TEST");
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub		
		return super.toString() + " Instance : " + nbInstance + " Call : " + nbCall + " " + this.hashCode();
	}
	
	public static void main(String[] args) {		
		Singleton s1 = newSingleton();
		s1.start();
		Singleton s2 = newSingletonBis();
		s2.start();
		
		System.out.println(s1);
		System.out.println(s2);
	}
}
